{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# mysql 连接与操作\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymysql\n",
    "# 打开数据库连接\n",
    "# 用户名:hp, 密码:Hp12345.,用户名和密码需要改成你自己的mysql用户名和密码，并且要创建数据库TESTDB，并在TESTDB数据库中创建好表Student\n",
    "db = pymysql.connect(host='127.0.0.1',\n",
    "                     user='root',\n",
    "                     password='mysql',\n",
    "                     database='test')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(('AGENT_CODE', 253, None, 200, 200, 0, True), ('AGENT_NAME', 253, None, 1020, 1020, 0, True))\n",
      "(('020501', '刘文昊'), ('020502', '胡宏伟'), ('020503', '陆雨晴'), ('020504', '马冬梅'), ('020505', '肖雅婷'))\n"
     ]
    }
   ],
   "source": [
    "# 使用cursor()方法获取操作游标\n",
    "cursor = db.cursor()\n",
    "sql = 'select * from AGENT;'\n",
    "\n",
    "# 使用 execute()  方法执行 SQL 查询\n",
    "cursor.execute(sql)\n",
    "# cursor.execute(\"SELECT VERSION()\")\n",
    "\n",
    "# 使用 fetchone() 方法获取单条数据.\n",
    "data = cursor.fetchall()\n",
    "# for row in data:\n",
    "#     print (row)\n",
    "# 获取字段名称\n",
    "ziduan = cursor.description\n",
    "print(ziduan)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 当然你也可以使用pandas来读取数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AGENT_CODE</th>\n",
       "      <th>AGENT_NAME</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>020501</td>\n",
       "      <td>刘文昊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>020502</td>\n",
       "      <td>胡宏伟</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>020503</td>\n",
       "      <td>陆雨晴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>020504</td>\n",
       "      <td>马冬梅</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>020505</td>\n",
       "      <td>肖雅婷</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  AGENT_CODE AGENT_NAME\n",
       "0     020501        刘文昊\n",
       "1     020502        胡宏伟\n",
       "2     020503        陆雨晴\n",
       "3     020504        马冬梅\n",
       "4     020505        肖雅婷"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd \n",
    "data = pd.read_sql(sql=sql,con=db)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "db.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
